<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="120px">
                <el-form-item label="拓展名称" prop="name">
                    <el-input
                        v-model="formData.name"
                        placeholder="请输入拓展名称"
                        clearable
                    />
                </el-form-item>
                <el-form-item label="链接地址" prop="link">
                    <el-input
                        v-model="formData.link"
                        placeholder="请输入链接地址"
                        clearable
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import Popup from '@/components/popup/index.vue'
import type { FormInstance } from 'element-plus'
import { videoDetils, setVideo } from '@/api/application/website/swiper'
const mode = ref('add')
const route = useRoute()
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑轮播图拓展' : '新增轮播图拓展'
})
const formData = reactive({
    id:0,
    name:"",
    link:"",
    chart_id:route.query.id
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const handleSubmit = async () => {
    await formRef.value?.validate()
    await setVideo(formData)
    popupRef.value?.close()
    emit('success')
}
const getDetail = async (row: Record<string, any>) => {
    const data = await videoDetils({
        id: row.id
    })
    setFormData(data)
}
const setFormData = (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}
const handleClose = () => {
    emit('close')
}
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
